package cn.catfish.service.impl;

import cn.catfish.entity.User;
import cn.catfish.mapper.UserMapper;
import cn.catfish.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public List<User> getAllUser() {
        List<User> users = userMapper.findAll();
        System.out.println("查询到的用户数量: " + users.size());
        return users;
    }

    @Override
    @Cacheable(value = "user",key = "#id",unless = "#result == null")
    public User getUserById(int id) {
        return userMapper.findById(id);
    }

    @Override
    @Cacheable(value = "user",key = "#user.userid")
    public void insertUser(User user) {
        userMapper.insert(user);
    }

    @Override
    @CachePut(value = "user",key = "#user.userid")
    @CacheEvict(value = "user",allEntries = true)
    public User updateUser(User user) {
        userMapper.update(user);
        return user;
    }

    @Override
    @CacheEvict(value = "user",key = "#userid")
    public void deleteUser(int userid) {
        userMapper.delete(userid);
    }
}
